package com.zzjson.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2020 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.socket</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2020年02月14日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class ServerSocketDemo {
	public static void main(String[] args) {
		ServerSocket serverSocket = null;

		try {
			//服务端一定要监听一个端口号
			serverSocket = new ServerSocket(8080);

			//接收客户端的连接
			Socket accept = serverSocket.accept();

			//获取输入流
			BufferedReader in = new BufferedReader(new InputStreamReader(accept.getInputStream()));

			PrintWriter printWriter = new PrintWriter(accept.getOutputStream());

			//系统输入
			BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
			String line = sysin.readLine();
			while (!"bye".equals(line)) {
				System.out.println(line);
				printWriter.write(line);
				line = sysin.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
